' This program exported from BASIC Anywhere Machine (Version [5.2.3].[2024.07.14.16.44]) on 2024.07.14 at 19:51 (Coordinated Universal Time)
' Program by Charlie Veniot
'
' The challenge from bplus:
' What numbers between 1 and 1000 can NOT be expressed as the sum of consecutive integers?
' ie 15 = 7 + 8
' 18 = 5 + 6 + 7
'
' so neither one of these are in the set of numbers that can NOT be expressed as a sum of consecutive integers.
'
' BONUS: one number can be expressed 15 different ways as a sum of consecutive numbers what is it and list the ways.
FUNCTION HaveHit%( n%, i%, imax%, BYREF v$ )
DIM sum% = 0
HaveHit% = FALSE
IF i% <> imax% THEN
FOR l% = i% TO imax%
sum% = sum% + l%
v$ = v$ + l% + "+"
IF sum% > n% OR sum% = n% THEN l% = imax%
IF sum% = n% THEN HaveHit% = TRUE
NEXT
END IF
v$ = "(" + LEFT$( v$, LEN( v$ )-1 ) + ")"
END FUNCTION
SCREEN _NEWIMAGE(1024,216,0)
DIM result$ = ""
FOR n% = 1 to 1000
imax% = FIX( (n% + 1) / 2 )
c% = 0
FOR i% = 1 TO imax%
v$ = ""
IF HaveHit%( (n%), (i%), (imax%) , v$) THEN
c% = c% + 1
END IF
NEXT i%
IF c% = 0 THEN PRINT n% + " cannot be expressed as the sum of consecutive integers"
IF c% = 15 THEN
PRINT n% + " can be expressed 15 different ways as a sum of consecutive numbers"
FOR i% = 1 TO imax%
v$ = ""
IF HaveHit%( (n%), (i%), (imax%) , v$) THEN
PRINT " " + v$
END IF
NEXT i%
END IF
NEXT n%